package ads.pin.pinmon.scenes;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ads.pin.pinmon.dal.CreatureDAO;
import ads.pin.pinmon.dal.PlayerCreatureDAO;
import ads.pin.pinmon.dal.PlayerDAO;
import ads.pin.pinmon.entity.Creature;
import ads.pin.pinmon.entity.Player;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class PinmonScene extends Scene {

	private Player m_player;
	private CreatureDAO m_creatureDAO;
	private Creature m_creature;
	private List<Creature> m_creatureList;
	private PlayerCreatureDAO m_playerCreatureDAO;
	private int m_index;
	private int m_lastIndex;

	public PinmonScene(Game game, Player player) {
		this.m_game = game;
		this.m_spriteBatch = new SpriteBatch();
		this.m_player = player;
		m_index = 0;
		m_creatureDAO = new CreatureDAO();
		m_creatureList = new ArrayList<Creature>();

		getMaxCreatureId();
		initCreature();

	}

	/**
	 * Inicializa a variavel m_lastIndex para o valor do maior Id no banco.
	 */
	private void getMaxCreatureId() {
		try {
			Creature creature;
			creature = m_creatureDAO.searchByMaxId();
			m_lastIndex = creature.getId();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Inicializa a criatura que ficara ativa no momento.
	 */
	private void initCreature() {
		try {
			m_creature = m_creatureDAO.searchById(m_index);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void update(float deltaTime) {

		pollInput();

	}

	/**
	 * Verifica a tecla pressionada e age de acordo.
	 * 
	 */
	private void pollInput() {
		if (Gdx.input.isKeyPressed(Input.Keys.ENTER)) {

			// Verifica o numero de criaturas escolhidas.
			if (m_creatureList.size() < 3) {
				m_player.getCreatures().add(m_creature);

			} else {
				// Salva o jogador no banco de dados.
				PlayerDAO playerDAO = new PlayerDAO();

				try {
					playerDAO.save(m_player);
					m_player = playerDAO.searchLastId();
					m_playerCreatureDAO.savePlayerCreature(m_player);
				} catch (SQLException e) {
					// TODO Tratar com excecao propria.
					e.printStackTrace();
				}

				m_game.setScreen(new GameScene(m_game, m_player));
			}

		}

		// Verifica se ainda existem criaturas para a esquerda.
		if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
			if (m_index > 0) {
				--m_index;
				initCreature();
			}
		}

		// Verifica se ainda existem criaturas para a direita.
		if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
			if (m_index >= m_lastIndex) {
				m_index = 0;
				initCreature();

			} else {
				++m_index;
				initCreature();
			}
		}

		if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
			m_game.setScreen(new PlayerScene(m_game));
		}
	}

	@Override
	public void draw(float deltaTime) {
		// TODO Auto-generated method stub

	}

}
